<?php
class Mvc_LanguageObject extends ArrayObject{
	
	protected $SEPERATOR = ".";
	
	protected $value;
	protected $key;
	
	public static $invalid = array();
	
	public function __construct($value, $key = ""){
		$this->value = $value;
		$this->key = $key;
		
		if(is_array($value)){
			foreach($value as $index => $item){
				$this->offsetSet($index, $item, $key.$index);
			}
		}
	}
	
	public function __toString(){
		$key = ltrim("{$this->key}", $this->SEPERATOR);
		$return = "NAV: {$key}";
		self::$invalid[] = $return;
		return $return;
	}
	
	public function offsetGet($index){
		if( parent::offsetExists($index) )
			return parent::offsetGet($index);
		
		return new Mvc_LanguageValueMissing($index, "{$this->key}");
	}
	
	public function hasErrors(){
		return count(self::$invalid) > 0;
	}
	
	public function getInvalid(){
		return self::$invalid;
	}
	
	public function offsetSet($index, $newval, $key = ""){
		if( is_array($newval) ){
			parent::offsetSet($index, new Mvc_LanguageObject($newval, "{$this->key}{$this->SEPERATOR}{$index}"));
		}else{
			parent::offsetSet($index, new Mvc_LanguageValue($newval, "{$this->key}{$this->SEPERATOR}{$index}"));
		}
	}
}